<?php

$mcrp = './';
include_once('functions/session.php');
include_once('skin/default.php');
include_once('functions/categories.php');
include_once('functions/contests.php');

$SkinTitle = 'µContest - Liste des épreuves';
	
include('skin/header.php');

$contests = array();

if(isset($_GET['id']) && is_numeric($_GET['id']))
{
	if($_GET['id'] == -1)
	{
		$contests = GetAllContests();
		$category = new CategoryInfos;
		$category->title = 'Toutes les épreuves';
		$categories = GetAllCategories();
	}
	else
	{
		$contests = GetContestsByCategoryId($_GET['id']);
		$category = GetCategoryById($_GET['id']);
	}
}
else
{
	$categories = GetAllCategories();
}

$UsersCount = GetActiveUsersCount();

function ReplaceAccent($str, $charset='utf-8')
{
    $str = htmlentities($str, ENT_NOQUOTES, $charset);
    
    $str = preg_replace('#&([A-za-z])(?:acute|cedil|circ|grave|orn|ring|slash|th|tilde|uml);#', '\1', $str);
    $str = preg_replace('#&([A-za-z]{2})(?:lig);#', '\1', $str); // pour les ligatures e.g. '&oelig;'
    $str = preg_replace('#&[^;]+;#', '', $str); // supprime les autres caractères
    
    return $str;
}
?>

<div class="mc_box">
	<?php
		if(!isset($category)) echo '<h3>Liste des épreuves</h3>';
		else				  echo '<h3><a href="contests.php">Liste des épreuves</a> :: ' . stripslashes($category->title) . ($mcuser->isadmin?('<a href="admin/categories.php?id=' . $category->id . '"><span style="padding-left:10px; font-size:.6em;">Edit</a></a>'):'') . '</h3>';
	?>
	<div class="mc_contest_view_text">
		<?php if(isset($_GET['id']))
		{
		?>
			<center><table width="80%" id="mc_contests_list">
				<tr>
					<th class="mc_contests_list_name">Nom</th>
					<th width="30px" class="mc_contests_list_validated"></th>
					<?php if($category->id == -1) { ?>
					<th width="120px">Catégorie</th>
					<?php } ?>
					<th width="10%">Points</th>
					<th width="17%">Validations (%)</th>
				</tr>
				<?php
				foreach($contests as $c)
				{
					?>
					<tr>
						<td class="mc_contests_list_name"><a href="<?php echo 'contest.php?id=' . $c->id; ?>"><?php echo stripslashes($c->title) . ' (' . $c->id . ')'; ?></a></td>
						<td width="30px" class="mc_contests_list_validated"><?php if(IsValidated($mcuser, $c->id)) echo '<img src="img/check.png" />'; ?></td>
						<?php if($category->id == -1) { ?>
						<td width="120px"><?php echo '<a href="contests.php?id=' . $categories[$c->category]->id . '">' . stripslashes($categories[$c->category]->title) . '</a>'; ?></td>
						<?php } ?>
						<td width="10%"><?php echo $c->points; ?></td>
						<td width="17%"><img src="img/c/c<?php echo round(($c->validationcount/$UsersCount)*100,0); ?>.png"></td>
					</tr>
					<?php
				}
				if($mcuser->isadmin == true)
				{
					if($_GET['id'] == -1) $contests = GetAllContests(false);
					else $contests = GetContestsByCategoryId($_GET['id'], false);
					foreach($contests as $c)
					{
						?>
						<tr class="mc_admin_row">
							<td class="mc_contests_list_name"><a href="<?php echo 'contest.php?id=' . $c->id; ?>"><?php echo stripslashes($c->title) . ' (' . $c->id . ')'; ?></a></td>
							<td width="30px" class="mc_contests_list_validated"><?php if(IsValidated($mcuser, $c->id)) echo '<img src="img/check.png" />'; ?></td>
							<?php if($category->id == -1) { ?>
							<td width="120px"><?php echo '<a href="contests.php?id=' . $categories[$c->category]->id . '">' . stripslashes($categories[$c->category]->title) . '</a>'; ?></td>
							<?php } ?>
							<td width="10%"><?php echo $c->points; ?></td>
							<td width="17%"><img src="img/c/gc<?php echo round(($c->validationcount/$UsersCount)*100,0); ?>.png"></img></td>
						</tr>
						<?php
					}
					?>
					<tr><td colspan="5" style="font-size:.6em;color:#BBBBBB;"><a href="admin/contests.php?catid=<?php echo $category->id; ?>&newcontest=">Nouveau</a></td></tr>
					<?php
				}
				?>
			</table></center>			
			<?php
		}
		else
		{
			?>
			<div class="mc_category_list_item">
				<img src="img/all.png" />
				<h4><a href="contests.php?id=-1">Toutes les épreuves</a></h4>
			</div>
			<br />
			<?php
			foreach($categories as $c)
			{
			?>
				<div class="mc_category_list_item">
					<img src="img/<?php echo ReplaceAccent($c->title); ?>.png" />
					<h4><a href="<?php echo 'contests.php?id=' . $c->id; ?>"><?php echo stripslashes($c->title); ?></a><?php if($mcuser->isadmin) echo '<a href="admin/categories.php?id=' . $c->id . '"><span style="padding-left:10px; font-size:.6em;">Edit</a></a>'; ?></h4>
					<p><?php echo nl2br(stripslashes($c->description)); ?></p>
				</div>
			<?php
			}
		}
		?>
	</div>
</div>	

<?php

include('skin/footer.php');

?>